package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.dictionary.dao.db.CountryArea;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.ELogisticsAreaScope;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.code.config.dao.db.LogisticsCostConfig;
import com.gzhryc.shared_device.oem.code.config.services.LogisticsCostConfigService;
import com.gzhryc.shared_device.oem.code.config.services.dto.LogisticsCostConfigSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class LogisticsCostConfigController {

    static Logger log = Logger.getLogger(LogisticsCostConfigController.class);

    @Route(value = { "/config/logistics_cost/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("areaScopeList",ELogisticsAreaScope.values());
        return "/config/logistics_cost/list.html";
    }

    @Route(value = "/config/logistics_cost/list.data")
    public ListResult<LogisticsCostConfig> listData(HttpServletRequest request) {
        ListResult<LogisticsCostConfig> result = new ListResult<LogisticsCostConfig>(DtoConstants.NULL_DATA, "暂无数据");
        LogisticsCostConfigSearch search = WebUtils.getParameter(request, LogisticsCostConfigSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(LogisticsCostConfigService.self().countJoinBySearch(search));
        List<LogisticsCostConfig> data = LogisticsCostConfigService.self().findJoinBySearch(search, pageNo, pageLimit);
        if(data != null && data.size() > 0) {
            result.getBody().setData(data);
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/config/logistics_cost/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("areaScope",ELogisticsAreaScope.Country.index());    //默认
        request.setAttribute("provinceList",CountryAreaUtils.getCountryAreaList());
        return "/config/logistics_cost/edit.html";
    }

    @Route(value = { "/config/logistics_cost/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增物流价格配置失败");
        try {
        	LogisticsCostConfig logisticsCostConfig = WebUtils.getParameter(request, LogisticsCostConfig.class);
        	if(StringTools.isBlank(logisticsCostConfig.getCompanyId())) {
        		result.setMsg("请选择对应的物流公司");
                return result;
        	}
            if(ELogisticsAreaScope.Province.index().equals(logisticsCostConfig.getAreaScope())){
                if(StringTools.isBlank(logisticsCostConfig.getProvince())){
                    result.setMsg("请选择对应的省份");
                    return result;
                }
                
                CountryArea countryArea = CountryAreaUtils.getProvince(logisticsCostConfig.getProvince());
                if(countryArea != null) {
                	logisticsCostConfig.setAreaCode(countryArea.getId().toString());
                }
            }
            Double moneyStr = WebUtils.getDouble(request,"moneyStr",0D);
            if(moneyStr == null || moneyStr < 0 || moneyStr > 300){
                result.setMsg("物流价格请设置在0-300元以内");
                return result;
            }
            Double freeMinMoneyStr = WebUtils.getDouble(request,"freeMinMoneyStr",0D);
            if(freeMinMoneyStr == null || freeMinMoneyStr < 0 || freeMinMoneyStr > 9999){
                result.setMsg("免邮客单价请设置在0-9999元以内");
                return result;
            }

            logisticsCostConfig.setMoney(NumberTools.changeMoney(moneyStr));
            logisticsCostConfig.setFreeMinMoney(NumberTools.changeMoney(freeMinMoneyStr));

            if(LogisticsCostConfigService.self().add(logisticsCostConfig)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setBody(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/config/logistics_cost/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request, "id");
		if(id != null) {
	        LogisticsCostConfig logisticsCostConfig = LogisticsCostConfigService.self().getJoinById(id);
	        WebUtils.setAttribute(request, logisticsCostConfig);
		}
        request.setAttribute("provinceList", CountryAreaUtils.getCountryAreaList());
        return "/config/logistics_cost/edit.html";
    }

    @Route(value = { "/config/logistics_cost/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改物流价格配置失败");
        LogisticsCostConfig logisticsCostConfig = WebUtils.getParameter(request, LogisticsCostConfig.class);
        try {
            if(ELogisticsAreaScope.Province.index().equals(logisticsCostConfig.getAreaScope())){
                if(StringTools.isBlank(logisticsCostConfig.getProvince())){
                    result.setMsg("请选择对应的省份");
                    return result;
                }
            }
            Double moneyStr = WebUtils.getDouble(request,"moneyStr",0D);
            if(moneyStr == null || moneyStr < 0 || moneyStr > 300){
                result.setMsg("物流价格请设置在0-300元以内");
                return result;
            }
            Double freeMinMoneyStr = WebUtils.getDouble(request,"freeMinMoneyStr",0D);
            if(freeMinMoneyStr == null || freeMinMoneyStr < 0 || freeMinMoneyStr > 9999){
                result.setMsg("免邮客单价请设置在0-9999元以内");
                return result;
            }

            logisticsCostConfig.setMoney(NumberTools.changeMoney(moneyStr));
            logisticsCostConfig.setFreeMinMoney(NumberTools.changeMoney(freeMinMoneyStr));

            if(LogisticsCostConfigService.self().updateNotNull(logisticsCostConfig)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setBody(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/config/logistics_cost/del.do" })
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除物流价格配置失败");
        Long id = WebUtils.getLong(request, "id");
        try {
            if(LogisticsCostConfigService.self().deleteById(id)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setBody(e.getMessage());
        }
        return result;
    }
}
